<template>
  <div>
    <div class="app-container" v-show="showExamList">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="考试名称" prop="examinationName">
          <el-input
            v-model="queryParams.examinationName"
            placeholder="请输入考试名称"
            clearable
            size="small"
          />
        </el-form-item>

        <el-form-item label="场次名称" prop="sessionName">
          <el-input
            v-model="queryParams.sessionName"
            placeholder="请输入场次名称"
            clearable
            size="small"
          />
        </el-form-item>

        <el-form-item label="时间" prop="startTime">
          <!-- <el-input
            v-model="queryParams.startTime"
            placeholder="请输入开始时间"
            clearable
            size="small"
          /> -->
          <el-date-picker
            v-model="queryParams.timer"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['exam:invigilate:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['exam:invigilate:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['exam:invigilate:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['exam:invigilate:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row> -->

      <el-table
        v-loading="loading"
        :data="invigilateList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="考试名称"
          align="center"
          prop="examinationName"
          width="280"
        />
        <el-table-column label="场次名称" align="center" prop="sessionName" />
        <el-table-column label="开始时间" align="center" prop="startTime" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="checkExam(scope.row)"
              >查看</el-button
            >
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['exam:invigilate:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['exam:invigilate:remove']"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改监考管理对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="监考教师id" prop="userId">
            <el-input v-model="form.userId" placeholder="请输入监考教师id" />
          </el-form-item>
          <el-form-item label="考试场次id" prop="sessionId">
            <el-input v-model="form.sessionId" placeholder="请输入考试场次id" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>

    <div class="student_box" v-if="!showExamList && !showExamPaper">
      <el-form
        :model="searchData"
        ref="queryForm"
        :inline="true"
        label-width="80px"
      >
        <el-form-item label="学号" prop="userName">
          <el-input
            v-model="searchData.userName"
            placeholder="请输入学生学号"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input
            v-model="searchData.nickName"
            placeholder="请输入学生姓名"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="searchStudent"
            >搜索</el-button
          >
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="checkExam(selectExam)"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <div class="exam_msg">
        <div>考场名称： {{ selectExam.examinationName }}</div>
        <div>开始时间： {{ selectExam.startTime }}</div>
        <div>结束时间：{{ selectExam.endTime }}</div>
      </div>
      <el-row class="student_msg">
        <el-col v-for="(item, index) in studentList" :key="index">
          <div class="student">
            <div>
              <div class="stu_key">姓名：</div>
              <div class="stu_value" style="border-radius: 0 20px 0 0">
                {{ item.nickName }}
              </div>
            </div>
            <div>
              <div class="stu_key">学号：</div>
              <div class="stu_value" style="color:#ffba00;">{{ item.userName }}</div>
            </div>
            <div class="stu_exam">
              考试状态：
              <span v-if="item.state == 0">【新增未分配试卷】</span>
              <span v-if="item.state == 1">【已分配试卷】</span>
              <span v-if="item.state == 2">【已开始考试】</span>
              <span v-if="item.state == 3" style="color: green"
                >【已批阅完成】</span
              >
              <span v-if="item.state == 5" style="color: red"
                >【考试状态异常】</span
              >
            </div>

            <div class="stu_delay">
              <el-button type="primary"  @click="delayExamBtn(item)"
                >延时</el-button
              >
               <el-button type="primary" @click="showPaper(item)"
                >查看考生试卷</el-button
              >
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <exam v-if="showExamPaper" :row="templateinfo" @parentEvent="parentEvent"></exam>

    <el-dialog
      :title="title"
      :visible.sync="showDelay"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名：" prop="nickName">
          <el-input
            v-model="delayExam.nickName"
            placeholder="姓名："
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="学号：" prop="userName">
          <el-input
            v-model="delayExam.userName"
            placeholder="学号："
            :disabled="true"
          />
        </el-form-item>
        <!-- <el-form-item label="延时考生" prop="sessionId">
          <el-input v-model="delayExam.examineeId" placeholder="延时考试ID" />
        </el-form-item> -->
        <el-form-item label="延时时长" prop="time">
          <el-input
            v-model="delayExam.time"
            placeholder="请输入延时时长（单位：分钟）"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="confirmDelay">确 定</el-button>
        <el-button @click="showDelay = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvigilate,
  getInvigilate,
  delInvigilate,
  addInvigilate,
  updateInvigilate,
  exportInvigilate,
  getInvigilateList,
  addDelayed,
} from "@/api/exam/invigilate";
import exam from "./exam.vue";

export default {
  name: "Invigilate",
  components: { exam },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监考管理表格数据
      invigilateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examinationName: null,
        sessionName: null,
        timer: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // 显示考场监控
      showExamList: true,
      // 监控学生信息
      studentList: [],
      // 选中的行数据
      selectExam: {},
      // 搜素学生信息
      searchData: {
        userName: "",
        nickName: "",
      },
      showDelay: false, // 显示延时弹窗
      // 延时考试
      delayExam: {
        nickName: "",
        userName: "",
      },
      templateinfo: [],
      showExamPaper: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询监考管理列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.timer != null) {
        this.queryParams.startTime = this.queryParams.timer[0];
        this.queryParams.endTime = this.queryParams.timer[1];
        delete this.queryParams.timer;
      }
      listInvigilate(this.queryParams).then((response) => {
        this.invigilateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查看考生试卷
    showPaper(row){
      console.log(" showPaper  row: ",row);
      this.templateinfo = row;
      this.showExamPaper=true;
    },
     parentEvent(message) {
      this.showExamPaper = false;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        sessionId: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监考管理";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInvigilate(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监考管理";
      });
    },

    // 查看监控
    checkExam(row) {
      this.selectExam = row;
      this.showExamList = false;
      this.searchData = {
        userName: "",
        nickName: "",
      };
      getInvigilateList({
        sessionId: row.sessionId,
      }).then((res) => {
        console.log(res);
        this.studentList = res.data.data;
      });
    },

    // 搜索学生
    searchStudent() {
      getInvigilateList(this.searchData).then((res) => {
        this.studentList = res.data.data;
      });
    },

    // 延时按钮
    delayExamBtn(item) {
      this.showDelay = true;
      this.delayExam.userName = item.userName;
      this.delayExam.nickName = item.nickName;
      this.delayExam.examineeId = item.sessionId;
    },

    // 确认延时
    confirmDelay() {
      addDelayed({
        examineeId: this.delayExam.examineeId,
        time: this.delayExam.time,
      }).then((res) => {
        console.log(res);
        this.$message({
          message: "延时成功",
          type: "success",
        });
        this.showDelay = false;
        this.delayExam.time = "";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        debugger;
        if (valid) {
          if (this.form.id != null) {
            updateInvigilate(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvigilate(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除监考管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInvigilate(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有监考管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInvigilate(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.student_box {
  padding: 20px;
  .exam_msg {
    display: flex;
    justify-content: space-around;
    margin-bottom: 10px;
    background: white;
    padding: 10px 0;
    border: 1px solid #ccc;
  }
  .student_msg {
    display: flex;
    flex-wrap: wrap;
    .el-col-4 {
      width: max-content;
      width: auto;
      margin-right: 35px;
      margin-bottom: 20px;
    }
    .el-col {
      width: max-content;
      width: auto;
    }
  }
  .student {
    display: flex;
    flex-direction: column;
    margin: 15px;
    border: 1px solid #ccc;
    border-radius: 0 20px 0px 20px;
    > div {
      display: flex;
      height: 43px;
      margin-bottom: 2px;
      .stu_key {
        display: flex;
        align-items: center;
        width: 85px;
        height: 100%;
        background: #eeeaea;
        margin-right: 2px;
        padding-left: 10px;
      }
      .stu_value {
        display: flex;
        align-items: center;
        width: 155px;
        height: 100%;
        background: #eeeaea;
        margin-right: 2px;
        padding-left: 10px;
        color: #1890ff;
        font-size: 18px;
      }
    }

    .stu_exam {
      width: 242px;
      height: 43px;
      padding-left: 10px;
      line-height: 43px;
      background: #eeeaea;
    }
    .stu_delay {
      width: 242px;
      height: 43px;
      padding-left: 10px;
      line-height: 43px;
      background: #eeeaea;
      border-radius: 0 0 0 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      .el-button {
        width: 155px;
        font-size: 16px;
        height: 35px;
        line-height: 0px;
      }
    }
  }
}
</style>
